// pages/address/edit/edit.js
const wxapi = require("../../../utils/wxapi");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressMsg: null,
    region: null,
    validateForm: true,
    operateType: null,
    userInfo: null
  },

  /**
   * 省市区选择
   * @param {*} e 
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },

  /**
   * 提交fom表单
   * @param {*} e 
   */
  formSubmit(e) {
    const formValue = e.detail.value;
    let validateForm = true;
    for (const key in formValue) {
      if (Object.hasOwnProperty.call(formValue, key)) {
        const value = formValue[key];
        if (key !== 'defaultAddress' && !value) {
          validateForm = false;
          break;
        }
      }
    }
    if (validateForm) {
      this.data.addressMsg ? this.updateAddress(formValue) : this.addAddress(formValue)
    } else {
      wx.showModal({
        content: '填写所有信息',
        showCancel: false
      })
    }
  },

  /**
   * 添加收货地址
   */
  addAddress(formValue) {
    const headers = {
      Authorization: app.globalData.token
    }
    const bodyParams = Object.assign(formValue, { province: this.data.region[0], city: this.data.region[1], area: this.data.region[2] }, { defaultAddress: formValue.defaultAddress ? 0 : 1, cardNo: app.globalData.userInfo.cardNo })
    wxapi.addUserAddress(bodyParams, headers).then(res => {
      if (res.code === 20000) {
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2];
        if (this.data.operateType === 'select') {
          prevPage.setData({
            chooseAddress: bodyParams
          })
        } else {
          prevPage.getAddressList();
        }
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false
        })
      }
    })
  },

  /**
   * 更新收货地址
   */
  updateAddress(formValue) {
    const region = {
      province: this.data.region ? this.data.region[0] : this.data.addressMsg.province,
      city: this.data.region ? this.data.region[1] : this.data.addressMsg.city,
      area: this.data.region ? this.data.region[2] : this.data.addressMsg.area
    }
    const bodyParams = Object.assign(formValue, region, { defaultAddress: formValue.defaultAddress ? 0 : 1, cardNo: this.data.userInfo.stk, id: this.data.addressMsg.id })
    const headers = {
      Authorization: app.globalData.token
    }
    wxapi.updateAddress(bodyParams, headers).then(res => {
      if (res.code === 20000) {
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.type) {
      this.setData({
        operateType: options.type
      })
    }
    const eventChannel = this.getOpenerEventChannel();
    if (eventChannel && Object.keys(eventChannel).length > 0) {
      eventChannel.on('addressMsg', (addressMsg) => {
        if (addressMsg) {
          this.setData({
            addressMsg: addressMsg,
          });
          wx.setNavigationBarTitle({
            title: '编辑收货地址',
          })
        } else {
          wx.setNavigationBarTitle({
            title: '新建收货地址',
          })
        }
        this.data.userInfo = app.globalData.userInfo;
      })
    } else {
      wx.switchTab({
        url: '/pages/center/center',
      })
      return;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})